/**
 * 
 */
package com.chinamobile.maps.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chinamobile.maps.dao.IChannelnetworksDAO;
import com.chinamobile.maps.orm.Channelnetworks;

/**
 * @author FrankWong
 *
 */
public class ChannelnetworksDAOImpl extends HibernateDaoSupport implements
		IChannelnetworksDAO {

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#addChannelnetworks(com.chinamobile.maps.orm.Channelnetworks)
	 */
	@Override
	public void addChannelnetworks(Channelnetworks channelnetworks) {
		this.getHibernateTemplate().save(channelnetworks);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#delChannelnetworks(com.chinamobile.maps.orm.Channelnetworks)
	 */
	@Override
	public void delChannelnetworks(Channelnetworks channelnetworks) {
		this.getHibernateTemplate().delete(channelnetworks);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#delChannelnetworks(int)
	 */
	@Override
	public void delChannelnetworks(int id) {
		this.getHibernateTemplate().delete(getChannelnetworksById(id));
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#updateChannelnetworks(com.chinamobile.maps.orm.Channelnetworks)
	 */
	@Override
	public void updateChannelnetworks(Channelnetworks channelnetworks) {
		this.getHibernateTemplate().update(channelnetworks);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#getChannelnetworks()
	 */
	@Override
	public List<Channelnetworks> getChannelnetworks() {
		List list = this.getHibernateTemplate().find("from Channelnetworks");
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#getChannelnetworksById(int)
	 */
	@Override
	public Channelnetworks getChannelnetworksById(int id) {
		List list = this.getHibernateTemplate().find("from Channelnetworks as channelnetworks where channelnetworks.id=?",
				new Object[] { id });

		if (list != null && list.size() > 0) {
			return (Channelnetworks) list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.IChannelnetworksDAO#getObjects(java.lang.String)
	 */
	@Override
	public List<Channelnetworks> getObjects(String hsql) {
		List list = this.getHibernateTemplate().find(hsql);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public List<Channelnetworks> findByKeyword(String keyword) {
		String hql = "from Channelnetworks as cn where cn.name like '%" + 
		        keyword + "%' or cn.address like '%" + keyword + 
		        "%' or cn.channelCode like '%" + keyword + "%'";
		List<Channelnetworks> list = getObjects(hql);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public boolean deleteAll() {
		try {
			Session session = getSession();
			Transaction tx = this.getSession().beginTransaction();
			session.delete("from Channelnetworks");
			tx.commit();
			return true;
		} catch (RuntimeException re) {
			return false;
		}
	}
}
